<template>
  <el-dialog title="编辑员工" :visible.sync="visible" @closed="closeDialog">
    <el-form ref="form" :model="form" :form="form" :rules="rules">
      <el-form-item label="姓名" label-width="120px" prop="username">
        <el-input
          v-model="form.username"
          style="width:60%"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item prop="mobile" label="手机" :label-width="formLabelWidth">
        <el-input v-model="form.mobile" style="width:60%" autocomplete="off" />
      </el-form-item>
      <el-form-item
        prop="timeOfEntry"
        label="入职时间"
        :label-width="formLabelWidth"
      >
        <el-date-picker
          v-model="form.timeOfEntry"
          style="width:60%"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item
        prop="formOfEmployment"
        label="聘用形式"
        :label-width="formLabelWidth"
      >
        <el-select
          v-model="form.formOfEmployment"
          style="width:60%"
          placeholder="请选择活动区域"
        >
          <el-option
            v-for="item in optionData"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        prop="workNumber"
        label="工号"
        :label-width="formLabelWidth"
      >
        <el-input
          v-model="form.workNumber"
          style="width:60%"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item
        prop="departmentName"
        label="部门"
        :label-width="formLabelWidth"
      >
        <el-input
          v-model="form.departmentName"
          style="width:60%"
          autocomplete="off"
          @focus="showTrees"
        />
        <!-- @blur="closeTrees" -->
        <el-tree
          v-if="showTree"
          class="my-tree"
          :data="treeData"
          :props="defaultProps"
          @node-click="handleNodeClick"
        />
      </el-form-item>
      <el-form-item
        prop="correctionTime"
        label="转正时间"
        :label-width="formLabelWidth"
      >
        <el-date-picker
          v-model="form.correctionTime"
          style="width:60%"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item
        style="width:85%;text-align:right"
        :label-width="formLabelWidth"
      >
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button type="worry" @click="closeDialogHandle">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { filterArr } from '../../../utils/index'
import { getDepartments } from '@/api/departments'
import EmploeyyeEnum from '../../../api/constant/employees'
import { addEmployee } from '../../../api/employees'
export default {
  data() {
    return {
      visible: false,
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      formLabelWidth: '120px',
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showTree: false,
      optionData: EmploeyyeEnum.hireType,
      rules: {
        username: [
          { required: true, message: '输入内容不能为空', trigger: 'change' }
        ],
        mobile: [
          { required: true, message: '输入内容不能为空', trigger: 'change' }
        ],
        formOfEmployment: [
          { required: true, message: '输入内容不能为空', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '输入内容不能为空', trigger: 'change' }
        ],
        departmentName: [
          { required: true, message: '输入内容不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '输入内容不能为空', trigger: 'change' }
        ],
        correctionTime: [
          { required: true, message: '输入内容不能为空', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    handleNodeClick(data) {
      // console.log(data)
      this.form.departmentName = data.name
      this.showTree = false
    },
    // 显示tree 并获取数据渲染
    async showTrees() {
      this.showTree = true
      const res = await getDepartments()
      // console.log(res.depts)
      const arr = await filterArr(res.depts, '')
      // console.log(arr)
      // const filterRes = await filterArr(res.depts, '')
      this.treeData = arr
    },
    // 提交表单新增员工
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // console.log(this.form)
          const res = await addEmployee(this.form)
          console.log(res)
          this.visible = false
          this.$message.success('新增成功')
        } else {
          this.$message.error('校验失败，请重新输入')
          return false
        }
      })
    },
    closeDialog() {
      this.form = {}
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    closeDialogHandle() {
      this.visible = false
      this.form = {}
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    }
  }
}
</script>

<style>
.my-tree {
  width: 60%;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  position: absolute;
  z-index: 99;
}
</style>
